﻿package  
{
	
	/**
	 * ...
	 * @author Matthew McDole
	 */
	public class dna 
	{
		public var m_radius:Array = new Array();
		public var m_px:Array = new Array();
		public var m_py:Array = new Array();
		public var score:Number = -1;
		
		
		public function dna() 
		{
			
			// Randomly choose initial elements for Vehicle
			for (var i:int = 0; i < 4; i++)
			{
				m_radius.push(randRange(5, 20));
				m_px.push(randRange(0, 150));
				m_py.push(randRange(0, 100));
			}
				
			
		}
		
		public function mutate():void
		{
			for (var i:int = 0; i < 4; i++)
			{
				// 5% chance to mutate each element
				if (randRange(0, 100) > 95)
				{
					m_radius[i] = randRange(5, 25);
				}
				
				if (randRange(0, 100) > 95)
				{
					m_px[i] = randRange(0, 150);
				}
				
				if (randRange(0, 100) > 95)
				{
					m_py[i] = randRange(0, 100);
				}
				
			}
		}
		
		public static function cross(dnaSample1:dna, dnaSample2:dna):dna
		{
			var crossSample:dna = new dna();
			
			for (var i:int = 0; i < 4; i++)
			{
				crossSample.m_radius[i] = dna.crossVars(dnaSample1.m_radius[i], dnaSample2.m_radius[i]);
				crossSample.m_px[i] = dna.crossVars(dnaSample1.m_px[i], dnaSample2.m_px[i]);
				crossSample.m_py[i] = dna.crossVars(dnaSample1.m_py[i], dnaSample2.m_py[i]);
			}
			return crossSample;
		}
		
		public static function crossVars(gene1:Number, gene2:Number):Number
		{
			var rand = Math.random();
			return (rand * gene1 + (1 - rand) * gene2);
		}
		
		public static function randRange(start:Number, end:Number):Number
		{
			return Math.floor(start +(Math.random() * (end - start + 1)));
		}
		
		
		
		
		
	}
	
}